package 多线程.多线程创建.生产者消费者.同步方法;

public class BaoZiPu {

    public int count = 0;

    private boolean flag = false;

    public BaoZiPu(int count, boolean flag) {
        this.count = count;
        this.flag = flag;
    }

    public BaoZiPu() {
    }

    public synchronized int getCount() {
        while (!this.flag) {
            // 没有包子就等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println("消费第...." + count + "个包子.");
        flag = false;
        this.notifyAll();
        return count;
    }

    public synchronized void setCount() {
        while (this.flag) {
            // 有包子不用生产
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        count++;
        System.out.println("生产第" + count + "个包子.");
        this.flag = true;
        this.notifyAll();
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
